package com.laboratory.report.lab05.lab0504;

import lombok.AllArgsConstructor;
import lombok.SneakyThrows;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 测试
 */
public class Lab0504Main {

    private static AtomicInteger water = new AtomicInteger(1000);

    @SneakyThrows
    public static void main(String[] args) {
        ExecutorService es = Executors.newFixedThreadPool(4);
        Produce produce = new Produce();
        Game game1 = new Game("比赛1");
        Game game2 = new Game("比赛2");
        Game game3 = new Game("比赛3");
        es.submit(produce);
        es.submit(game1);
        es.submit(game2);
        es.submit(game3).get();
    }

    public static class Produce extends Thread {

        @SneakyThrows
        @Override
        public void run() {
            while(true) {
                if(water.get()<100) {
                    System.out.println("保障队送水成功，现有水"+water.addAndGet(1000)+"瓶");
                }
                Thread.sleep(100);
            }
        }
    }

    @AllArgsConstructor
    public static class Game extends Thread {

        private String gameName;

        @SneakyThrows
        @Override
        public void run() {
            while(true) {
                if(water.get()>=20) {
                    System.out.println(gameName + "拿走20瓶水，现场馆有水" + water.addAndGet(-20) + "瓶");
                }
                Thread.sleep(100);
            }
        }
    }
}
